package main;

import java.util.*;
import models.*;
import voidlib.*;
/**
 * @author nevidimov
 */
public class BanditController extends VoidController {
    
    public void listInvitesAction() {
        Bandit bandit = VoidUser.getCurrent().getSpecificUser();
        List<Invite> invites = new Invite().getMapper().fetchAll("bandit_id = ?", bandit.getUserId(), "status = ?", RequestStatus.Pending);
        this.view.put("invites", invites);
        this.initModelAndView();
    }
    
    public void createInviteAction() {
        Bandit bandit = VoidUser.getCurrent().getSpecificUser();
        if (bandit.getClan() == null) {
            this.setMessage("You have no clan, man");;
            this.redirect(VoidConfig.rootUrl);
        }
        List<Bandit> bandits = new Bandit().getMapper().fetchAll("user_id <> ?", bandit.getUserId());
        String banditIdStr = this.request.getParameter("to");
        if (banditIdStr != null) {
            int banditId = Integer.parseInt(banditIdStr);
            Bandit him = new Bandit().fetch(banditId);
            Bandit me = VoidUser.getCurrent().getSpecificUser();
            me.inviteToMyClan(banditId);
            this.setMessage("You invited " + him.getUser().getTitle() + " to your clan");
        }
        
        this.view.put("bandits", bandits);
        this.initModelAndView();
    }
    
    public void myClanAction() {
        Bandit bandit = VoidUser.getCurrent().getSpecificUser();
        Clan clan = bandit.getClan();
        if (clan != null) {
            List<Bandit> bandits = clan.getBandits();
            this.view.put("bandits", bandits);
        }
        
        this.view.put("clan", clan);
        this.initModelAndView();
    }
    
    public void createClanAction() {
        if (this.isPostRequest()) {
            String title = this._getParam("title");
            Bandit bandit = VoidUser.getCurrent().getSpecificUser();
            bandit.createClan(title);
            this.setMessage("You created your clan successfully");
            this.redirect(VoidConfig.rootUrl + "bandit.htm?act=myclan");
        }
        this.initModelAndView();
    }
    
    public void leaveClanAction() {
        Bandit bandit = VoidUser.getCurrent().getSpecificUser();
        bandit.leaveClan();
        this.setMessage("You leaved your clan successfully");
        this.redirect(VoidConfig.rootUrl);
    }
    
    public void acceptJoinClanAction() {
        int inviteId = Integer.parseInt(this._getParam("id"));
        Invite invite = new Invite().fetch(inviteId);
        Bandit bandit = VoidUser.getCurrent().getSpecificUser();
        Bandit owner = invite.getOwner().getSpecificUser();
        bandit.joinClan(owner.getClanId());
        invite._status = RequestStatus.OK;
        invite.save();
        this.redirect(VoidConfig.rootUrl);
        this.setMessage("You joined the clan " + owner.getClan().getTitle());
        this.redirect(VoidConfig.rootUrl + "bandit.htm?act=listinvites");
    }
    
    public void refuseJoinClanAction() {
        int inviteId = Integer.parseInt(this._getParam("id"));
        Invite invite = new Invite().fetch(inviteId);
        invite._status = RequestStatus.Refused;
        invite.save();
        this.setMessage("You refused to join clan");
        this.redirect(VoidConfig.rootUrl + "bandit.htm?act=listinvites");
    }
    
}
